<?php
namespace app\admin\controller;
use \think\Controller;
use \think\Cookie;
use think\Request;
use \think\Session;
class Deal extends Controller
{
    public function _initialize(){
        if(!isLogin()){
            $this->redirect('/trade/login');
        }
    }
    public function index(Request $request)
    {

        $list=false;
        //当前登录用户的id
        $id=Session::get('trade_user_id');

        $query=$request->param();

        $account=null;
        $name=null;
        $start_time=null;
        $end_time=null;

        if($id==1){
            //超级管理员查询入口

            //判断是否有参数
            if(count($query)>1){
                //只有在有参数时查询
                $account=$request->param('account') ? $request->param('account') : 'not null';
                $name=$request->param('name') ? $request->param('name') : 'not null';
                $start_time=$request->param('start_time') ? strtotime($request->param('start_time')) : '0';
                $end_time=$request->param('end_time') ? strtotime($request->param('end_time')) : strtotime(date('Y-m-d',time()));

                $list=db('deal')->alias('a')
                    ->field('a.id,a.account,a.user,a.order_num,a.goods,a.type,a.deal_price,a.deal_num,a.charge,a.time,b.custom_name as user_name')
                    ->join('__ADD_NAME__ b','a.account = b.account','left')
                    ->where('a.account',$account)
                    ->where('b.custom_name',$name)
                    ->where('a.time','between',[$start_time,$end_time])
                    ->order('a.id')
                    ->paginate(20,false,['query'=>$query]);
            }


        }else{
            //代理商查询入口

            //判断是否有参数
            if(count($query)>1){
                //只有在有参数时查询


                //查询旗下的子代理
                $childId=db('user')->where('pid',$id)->column('id');
                $childId[]= $id;

                //只有在有参数时查询
                $account=$request->param('account') ? $request->param('account') : 'not null';
                $name=$request->param('name') ? $request->param('name') : 'not null';
                $start_time=$request->param('start_time') ? strtotime($request->param('start_time')) : '0';
                $end_time=$request->param('end_time') ? strtotime($request->param('end_time')) : strtotime(date('Y-m-d',time()));


                $list=db('deal')->alias('a')
                    ->field('a.id,a.account,a.user,a.order_num,a.goods,a.type,a.deal_price,a.deal_num,a.charge,a.time,b.custom_name as user_name')
                    ->join('__ADD_NAME__ b','a.account = b.account','left')
                    ->where('a.pid','in',$childId)
                    ->where('a.account',$account)
                    ->where('b.custom_name',$name)
                    ->where('a.time','between',[$start_time,$end_time])
                    ->order('a.id')
                    ->paginate(20,false,['query'=>$query]);
            }


        }


        if($account == 'not null'){$account=null;}
        if($name == 'not null'){$name=null;}
        if($start_time == 'not null'){$start_time=null;}
        if($end_time == 'not null'){$end_time=null;}

        $this->assign('list',$list);
        $this->assign('account',$account);
        $this->assign('start_time',$request->param('start_time'));
        $this->assign('end_time',$request->param('end_time'));
        $this->assign('name',$name);
        return $this->fetch('list');


    }


}
